package com.zhuzi.guicollect.controller.fx;

import com.zhuzi.guicollect.Main;
import com.zhuzi.guicollect.ui.FavoriteView;
import com.zhuzi.guicollect.utils.AlertUtils;
import com.zhuzi.guicollect.utils.SysUtils;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.kordamp.bootstrapfx.BootstrapFX;
import org.kordamp.bootstrapfx.scene.layout.Panel;

/**
 * @Author: MI
 * @Date: 2021/10/31/22:29
 * @Description:
 */
@Slf4j
@FXMLController
public class LoginController extends Application {
    @FXML
    public TextField textUser;
    @FXML
    public PasswordField textPwd;

    @Override
    public void start(Stage stage) throws Exception {
        // FXMLLoader fxmlLoader = new FXMLLoader(SomeotherController.class.getResource("com/zhuzi/guicollect/ui/login.fxml"));
        // Scene scene = new Scene(fxmlLoader.load());
        // scene.getStylesheets().add(BootstrapFX.bootstrapFXStylesheet());

        Panel panel = new Panel("This is the title");
        panel.getStyleClass().add("panel-primary");                            //(2)
        BorderPane content = new BorderPane();
        content.setPadding(new Insets(20));
        Button button = new Button("Hello BootstrapFX");
        button.getStyleClass().setAll("btn", "btn-danger");                     //(2)
        content.setCenter(button);
        panel.setBody(content);

        Scene scene = new Scene(panel);
        scene.getStylesheets().add(BootstrapFX.bootstrapFXStylesheet());       //(3)


        stage.setTitle("Hello!");
        stage.setScene(scene);

        stage.getScene().getStylesheets().add(BootstrapFX.bootstrapFXStylesheet());
        stage.setResizable(false);

        //设置图标

    }


    /**
     * 登录
     *
     * @param actionEvent
     */
    public void onLogin(ActionEvent actionEvent) {

        String pwd = textPwd.getText();
        String user = textUser.getText();
        if (StringUtils.isAnyBlank(pwd, user)) {
            AlertUtils.showAlter(Alert.AlertType.INFORMATION, "请输入信息");
            return;
        }
        if (SysUtils.userLogin(user, pwd)) {
            AlertUtils.showAlter(Alert.AlertType.INFORMATION, "登录成功");
            Main.sysShowView(FavoriteView.class);
        } else {
            Alert dlg = new Alert(Alert.AlertType.WARNING, "");
            dlg.initModality(Modality.WINDOW_MODAL);
            dlg.getDialogPane().setContentText("登录失败");
            dlg.getDialogPane().setHeaderText("提示");
            dlg.showAndWait();
        }

    }
}
